#include <tchar.h>
#include "win32oglDriver.h"

Win32oglDriver Win32oglDriver::m_Instance;

bool Win32oglDriver::Init()
{
	if(m_bInitialised)
		return true;

	TCHAR* strWnd = _T("WGL");
	WNDCLASS wc;
	PIXELFORMATDESCRIPTOR pfd;
	/* register window class */
	ZeroMemory(&wc, sizeof(WNDCLASS));
	wc.hInstance = GetModuleHandle(NULL);
	wc.lpfnWndProc = DefWindowProc;
	wc.lpszClassName = strWnd;
	if (0 == RegisterClass(&wc)) return GL_TRUE;
	/* create window */
	HWND wnd = CreateWindow(strWnd,strWnd, 0, CW_USEDEFAULT, CW_USEDEFAULT, 
		CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, 
		GetModuleHandle(NULL), NULL);
	if (NULL == wnd) return false;
	/* get the device context */
	HDC dc = GetDC(wnd);
	if (NULL == dc) return false;
	/* find pixel format */
	ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
	
	pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
	int visual = ChoosePixelFormat(dc, &pfd);
	if (0 == visual) return false;
	
	/* set the pixel format for the dc */
	if (FALSE == SetPixelFormat(dc, visual, &pfd))
		return false;
	/* create rendering context */
	HGLRC rc = wglCreateContext(dc);
	if (NULL == rc) return false;
	if (!wglMakeCurrent(dc, rc))
		return false;

	if( GLEW_OK != glewInit() ) 
		return false;

	InitSupportPixelFormats(dc);

	wglMakeCurrent(NULL,NULL);

	//destroy context
	if (NULL != rc) wglMakeCurrent(NULL, NULL);
	if (NULL != rc) wglDeleteContext(rc);
	if (NULL != wnd && NULL != dc) ReleaseDC(wnd, dc);
	if (NULL != wnd) DestroyWindow(wnd);
	UnregisterClass(strWnd, GetModuleHandle(NULL));


	m_bInitialised = true;
	return true;	
}

//
void Win32oglDriver::InitSupportPixelFormats(HDC hdc)
{	
	int iAttributes = WGL_NUMBER_PIXEL_FORMATS_ARB;
	int nPFD = 0;
	if (wglGetPixelFormatAttribivARB(hdc,0,0,1,&iAttributes,&nPFD) == GL_FALSE)
		return;

	Win32oglPixelFormat format;
	Win32oglPixelFormat::iAttribSetType& iAttribSet = format.m_iAttribSet;
	iAttribSet[WGL_PIXEL_TYPE_ARB];
	iAttribSet[WGL_COLOR_BITS_ARB];
	iAttribSet[WGL_RED_BITS_ARB];
	iAttribSet[WGL_GREEN_BITS_ARB];
	iAttribSet[WGL_BLUE_BITS_ARB];
	iAttribSet[WGL_ALPHA_BITS_ARB];
	iAttribSet[WGL_DEPTH_BITS_ARB];
	iAttribSet[WGL_STENCIL_BITS_ARB];
	iAttribSet[WGL_PIXEL_TYPE_ARB];
	iAttribSet[WGL_PIXEL_TYPE_ARB];
	iAttribSet[WGL_DRAW_TO_WINDOW_ARB];
	iAttribSet[WGL_DRAW_TO_BITMAP_ARB];
	iAttribSet[WGL_DRAW_TO_PBUFFER_ARB];
	iAttribSet[WGL_PIXEL_TYPE_ARB];
	iAttribSet[WGL_DOUBLE_BUFFER_ARB];
	iAttribSet[WGL_STEREO_ARB];
	iAttribSet[WGL_ACCELERATION_ARB];
	iAttribSet[WGL_NEED_PALETTE_ARB];
	iAttribSet[WGL_NEED_SYSTEM_PALETTE_ARB];
	iAttribSet[WGL_SWAP_LAYER_BUFFERS_ARB];
	iAttribSet[WGL_SWAP_METHOD_ARB];
	iAttribSet[WGL_NUMBER_OVERLAYS_ARB];
	iAttribSet[WGL_NUMBER_UNDERLAYS_ARB];
	iAttribSet[WGL_TRANSPARENT_ARB];
	iAttribSet[WGL_SUPPORT_GDI_ARB];
	iAttribSet[WGL_SUPPORT_OPENGL_ARB];
	
	iAttribSet[WGL_SAMPLES_ARB];
	iAttribSet[WGL_SAMPLE_BUFFERS_ARB];

	for(int i=0;i<nPFD;++i)
	{
		wglGetPixelFormatAttribivARB(hdc,i+1, 0, iAttribSet.size(), (int*)iAttribSet.GetKeys(), iAttribSet.GetValues() );
		format.invalidate();
		m_vSupportPixelFormat.push_back(format);
	}
	
}